/*
 * @lc app=leetcode.cn id=13 lang=javascript
 *
 * [13] 罗马数字转整数
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function(s) {
  let result = 0;
  let romanList = {
    "I": 1,
    "V": 5,
    "X": 10,
    "L": 50,
    "C": 100,
    "D": 500,
    "M": 1000,
  };
  let newStrArr = s.split(""); // 将s转成数组
  let index = 0;
  while (index < newStrArr.length) {
    if (
      (
        (newStrArr[index + 1] === "V" || newStrArr[index + 1] === "X") && newStrArr[index] === "I"
      ) ||
      (
        (newStrArr[index + 1] === "L" || newStrArr[index + 1] === "C") && newStrArr[index] === "X"
      ) || 
      (
        (newStrArr[index + 1] === "D" || newStrArr[index + 1] === "M") && newStrArr[index] === "C"
      )
      ) {
        result = romanList[newStrArr[index + 1]] - romanList[newStrArr[index]] + result;
        index += 2;
    } else {
      result += romanList[newStrArr[index]];
      index++;
    }
  }
  return result;
};
// @lc code=end

